package com.system.employee.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.system.common.util.Sex;
import com.system.employee.entity.Employee;


public class EmployeeDTO {
	private String empName;
	private Sex sex;	
	private Long partId;
	private String education;//学历
	private String political;//政治面貌
	private String ethnic;//民族
	private String userName;
	private Long postId;
	@DateTimeFormat(pattern = "yyyy/MM/dd")
	private Date joinTime;
	public String getEmpName() {
		return empName;
	}
	
	

	public Long getPostId() {
		return postId;
	}



	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public Sex getSex() {
		return sex;
	}

	public void setSex(Sex sex) {
		this.sex = sex;
	}

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}
	
	


	public Long getPartId() {
		return partId;
	}


	@JsonFormat(pattern = "yyyy/MM/dd",timezone="GMT+8")
	
	public Date getJoinTime() {
		return joinTime;
	}



	public void setJoinTime(Date joinTime) {
		this.joinTime = joinTime;
	}



	public void setPartId(Long partId) {
		this.partId = partId;
	}



	public String getPolitical() {
		return political;
	}

	public void setPolitical(String political) {
		this.political = political;
	}

	public String getEthnic() {
		return ethnic;
	}

	public void setEthnic(String ethnic) {
		this.ethnic = ethnic;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
   
	

	public void setPostId(Long postId) {
		this.postId = postId;
	}



	public static Specification<Employee> getSpecification(EmployeeDTO employeeDTO){
		Specification<Employee> spec = new Specification<Employee>() {
			public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != employeeDTO && !StringUtils.isEmpty(employeeDTO.getEmpName())) {
					 Predicate  p1 =  cb.like(root.get("empName").as(String.class),"%"+ employeeDTO.getEmpName()+ "%");
					 list.add(p1);
				 }
				 if(null != employeeDTO && null!=employeeDTO.getSex()) {
					 //if(employeeDTO.getSex()==Sex.male) {;}
					 Predicate  p2 =  cb.equal(root.get("sex").as(Sex.class),employeeDTO.getSex());
					 list.add(p2);
				 }
				 if(null != employeeDTO && !StringUtils.isEmpty(employeeDTO.getEducation())) {
					 Predicate  p3 =  cb.like(root.get("education").as(String.class),"%"+ employeeDTO.getEducation()+ "%");
					 list.add(p3);
				 }
				 if(null != employeeDTO && !StringUtils.isEmpty(employeeDTO.getPolitical())) {
					 Predicate  p4 =  cb.like(root.get("political").as(String.class),"%"+ employeeDTO.getPolitical()+ "%");
					 list.add(p4);
				 }
				 if(null != employeeDTO && !StringUtils.isEmpty(employeeDTO.getEthnic())) {
					 Predicate  p5 =  cb.like(root.get("ethnic").as(String.class),"%"+ employeeDTO.getEthnic()+ "%");
					 list.add(p5);
				 }
				 if(null != employeeDTO && !StringUtils.isEmpty(employeeDTO.getUserName())) {
					 Predicate  p6 =  cb.like(root.join("user").get("userName").as(String.class),"%"+ employeeDTO.getUserName()+ "%");
					 list.add(p6);
				 }
				 if(null != employeeDTO && !StringUtils.isEmpty(employeeDTO.getPartId())) {
					 Predicate  p7 = cb.equal(root.join("post").join("part").get("partId").as(Long.class), employeeDTO.getPartId()); 
					 list.add(p7);
					 
				 }
				 if(null != employeeDTO && employeeDTO.getPostId()!=null) {
					 Predicate  p8 =  cb.equal(root.join("post").get("postId").as(Long.class),employeeDTO.getPostId());
					 list.add(p8);
					 
				 }
				 if(null != employeeDTO && employeeDTO.getJoinTime()!=null) {
					 Predicate  p9 =  cb.lessThanOrEqualTo(root.get("joinTime").as(Date.class),employeeDTO.getJoinTime());
					 list.add(p9);
					 
				 }
				 
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
		
	}
}
